<?php
class EmailLinks extends Model_Abstract
{
	public function getRedirectUrl($emailId, $field) {
		try{
			$field = 'offer1_link';
			$sql = sprintf("select %s from email_links where email_id=? limit 1", $field);
			$result = $this->_connection->query($sql,array($emailId));
			if (count($result) > 0) {
				return $result[0][$field];
			}
			return false;
		}catch (Exception $e){
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function offerLinksIsRedirected($emailId) {
		try{
			$sql = "select redirect_date from email_links where email_id=? limit 1";
			$result = $this->_connection->query($sql,array($emailId));
			if (count($result) > 0) {
				return $result[0]['redirect_date'];
			}
			return false;
		}catch (Exception $e){
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function setRedirectDate($emailId, $offerOrder) {
		try{
			$sql = sprintf("update email_links set redirect_date=?, offer%d_click=1 where email_id=%d", $offerOrder, $emailId);
			return $this->_connection->execute($sql,array(date('YmdHis')));
		}catch (Exception $e){
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function addRedirectNum($emailId, $offerOrder) {
		try{
			$sql = 'update email_links set offer' . $offerOrder . '_click=offer' . $offerOrder.  '_click+1 where email_id=' . $emailId;
			return $this->_connection->execute($sql,array(date('YmdHis')));
		}catch (Exception $e){
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
}